package com.lpf.xypermission.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.lpf.xypermission.R;
import com.lpf.xypermission.aspectj.Ipermission;
import com.lpf.xypermission.util.PermissionUtil;

public class RequestPermissionActivity extends Activity {

    public static String PERMISSION = "permission";
    public static String REQUESTCODE = "requestcode";


    private static Ipermission mIpermission;
    private String tag = "RequestPermissionActivity";

    public static void startPermissionRequest(Context context, String[] permissions, int requestCode, Ipermission ipermission) {

        mIpermission = ipermission;

        Intent intent = new Intent(context, RequestPermissionActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        Bundle bundle = new Bundle();
        bundle.putStringArray(PERMISSION, permissions);
        bundle.putInt(REQUESTCODE, requestCode);

        intent.putExtras(bundle);

        context.startActivity(intent);

        if (context instanceof Activity) {
            ((Activity) context).overridePendingTransition(0, 0);
        }


    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        setContentView(R.layout.activity_main);

        Bundle bundle = getIntent().getExtras();
        String[] permission = bundle.getStringArray(PERMISSION);
        int requestcode = bundle.getInt(REQUESTCODE);

        requestPermission(permission, requestcode);

    }

    private void requestPermission(String[] permissions, int requestcode) {

        if (PermissionUtil.hasSelfPermissions(this, permissions)) {

            mIpermission.onPermissionGranted();

            Log.e(tag, "requestPermission>>>>>permissiongranted>>>>>>");
            finish();
            overridePendingTransition(0, 0);

        } else {


            Log.e(tag, "requestPermission>>>>>requestPermissions>>>>>>");

            ActivityCompat.requestPermissions(this, permissions, requestcode);

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        if (PermissionUtil.verifyPermissions(grantResults)) {

            Log.e(tag, "onRequestPermissionsResult>>>>>onPermissionGranted>>>>>>");
            mIpermission.onPermissionGranted();
        } else {

            if (PermissionUtil.shouldShowRequestPermissionRationales(this, permissions)) {
                //取消

                Log.e(tag, "onRequestPermissionsResult>>>>>onPermissionCanceled>>>>>>");
                mIpermission.onPermissionCanceled(requestCode);

            } else {
                //拒绝
                Log.e(tag, "onRequestPermissionsResult>>>>>onPermissionDenied>>>>>>");
                mIpermission.onPermissionDenied(requestCode);
            }
        }

        finish();
        overridePendingTransition(0, 0);


    }
}
